//
//  ByOrderShowVC.m
//  hutouben_ios
//
//  Created by mc on 16/11/30.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByOrderShowVC.h"
#import "ByNetOrderModel.h"
#import "ByOrderCell.h"
#import "ByOrderDetailVC.h"

@interface ByOrderShowVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataAry;
@end

@implementation ByOrderShowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [self fetchData];
}

#pragma makr ----getter
-(NSMutableArray *)dataAry{
    if (_dataAry == nil) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}
#pragma mark ----获取数据
- (void)fetchData{
    NSLog(@"%ld",self.order_state);
    ByNetOrderModel *model = [[ByNetOrderModel alloc] init];
    model.post_price = @"¥58";
    model.price = @"¥5867";
    ByNetOrderItemModel *itemModel = [[ByNetOrderItemModel alloc] init];
    itemModel.name = @"【汽车配件种类】汽车配件名称汽车配件名称汽车配件名称汽车配件名称汽车配件名称";
    itemModel.price = @"¥5888";
    itemModel.count = @"2345";
    model.items = @[itemModel];
    [self.dataAry addObject:model];
    
    ByNetOrderModel *model2 = [[ByNetOrderModel alloc] init];
    model2.post_price = @"¥22";
    model2.price = @"¥5867";

    ByNetOrderItemModel *itemModel2 = [[ByNetOrderItemModel alloc] init];
    itemModel2.name = @"【汽车配件种类】汽车配件名称汽车配件名称汽车配件名称汽车配件名称汽车配件名称";
    itemModel2.price = @"¥5888";
    itemModel2.count = @"2345";
    model2.items = @[itemModel2];
    [self.dataAry addObject:model2];
    
    ByNetOrderModel *model3 = [[ByNetOrderModel alloc] init];
    model3.post_price = @"¥508";
    model3.price = @"¥5867";

    ByNetOrderItemModel *itemModel3 = [[ByNetOrderItemModel alloc] init];
    itemModel3.name = @"【汽车配件种类】汽车配件名称汽车配件名称汽车配件名称汽车配件名称汽车配件名称";
    itemModel3.price = @"¥5888";
    itemModel3.count = @"2345";
    model3.items = @[itemModel3];
    [self.dataAry addObject:model3];
    
    [self.tableView reloadData];
}
#pragma mark ----创建视图
- (void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = HTB_BG_GRAY;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}

#pragma mark ---UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataAry.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ByNetOrderModel * orderModel = self.dataAry[section];
    return orderModel.items.count + 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetOrderModel * orderModel = self.dataAry[indexPath.section];
    NSInteger itemCount = orderModel.items.count;          //商品数量
    if (indexPath.row <itemCount) {
        ByOrderCell *cell = [ByOrderCell cellWithTableView:tableView];
        ByNetOrderItemModel *itemModel = orderModel.items[indexPath.row];
        cell.itemModel = itemModel;
        return cell;
    }
    else{
        static NSString *indentier = @"orderActionCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subviews in cell.subviews) {
            [subviews removeFromSuperview];
        }
        /**实付*/
        NSString *price = [NSString stringWithFormat:@"实付：%@",orderModel.price];
        CGFloat width = [price getSizeOfString:14].width + 10;
        UILabel *payLabel = [[UILabel alloc] init];
        [cell addSubview:payLabel];
        [payLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell).offset(10);
            make.top.equalTo(cell).offset(10);
            make.size.mas_equalTo(CGSizeMake(width, 25));
        }];
        payLabel.text = price;
        payLabel.font = FONT(14);
        payLabel.textColor = HTB_TEXT_COLOR2;
        
        
        /**待付款、已完成*/
        if (self.order_state == 0 || self.order_state == 3) {
            NSString *rightStr;
            if (self.order_state == 0) {
                rightStr = @"去支付";
            }else{
                rightStr = @"查看物流";
            }
            width = [rightStr getSizeOfString:14].width + 20;
            UIButton *rightBtn = [[UIButton alloc] init];
            [cell addSubview:rightBtn];
            [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(cell).offset(-10);
                make.top.equalTo(cell).offset(10);
                make.size.mas_equalTo(CGSizeMake(width, 25));
            }];
            [rightBtn setTitle:rightStr forState:UIControlStateNormal];
            [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            rightBtn.titleLabel.font = FONT(14);
            rightBtn.backgroundColor = HTB_THEME_COLOR;
            [rightBtn addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];

        }
        /**待发货*/
        else if (self.order_state == 1){
            
        }
        /**待收货*/
        else if (self.order_state == 2){
            width = [@"确认收货" getSizeOfString:14].width + 20;
            UIButton *sureBtn = [[UIButton alloc] init];
            [cell addSubview:sureBtn];
            [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(cell).offset(-10);
                make.top.equalTo(cell).offset(10);
                make.size.mas_equalTo(CGSizeMake(width, 25));
            }];
            [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [sureBtn setTitle:@"确认收货" forState:UIControlStateNormal];
            sureBtn.backgroundColor = HTB_THEME_COLOR;
            sureBtn.titleLabel.font = FONT(14);
            [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
            
            width = [@"查看物流" getSizeOfString:14].width + 20;
            UIButton *lookBtn = [[UIButton alloc] init];
            [cell addSubview:lookBtn];
            [lookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(sureBtn.mas_left).offset(-8);
                make.top.equalTo(cell).offset(10);
                make.size.mas_equalTo(CGSizeMake(width, 25));
                
            }];
            [lookBtn setTitle:@"查看物流" forState:UIControlStateNormal];
            [lookBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            lookBtn.titleLabel.font = FONT(14);
            lookBtn.backgroundColor = HTB_THEME_COLOR;
            [lookBtn addTarget:self action:@selector(lookAction:) forControlEvents:UIControlEventTouchUpInside];

            
            
        }
        
        
        return cell;
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetOrderModel * orderModel = self.dataAry[indexPath.section];
    NSInteger itemCount = orderModel.items.count;          //商品数量
    if (indexPath.row < itemCount) {
        return 100;
    }else{
        return 45;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
    headerView.backgroundColor = HTB_BG_GRAY;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, 30)];
    [headerView addSubview:contentView];
    contentView.backgroundColor = [UIColor whiteColor];
    
    CGFloat x,y,width,height;
    //运费
    ByNetOrderModel *model = self.dataAry[section];
    NSString *post_price = [NSString stringWithFormat:@"运费%@",model.post_price];
    width = [post_price getSizeOfString:14].width;
    x = SCREEN_WIDTH - width - 10;
    y = 7.5;
    height = 15;
    UILabel *postPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    postPriceLabel.text = post_price;
    postPriceLabel.font = FONT(14);
    postPriceLabel.textColor = HTB_ORDER_COLOR;
    [contentView addSubview:postPriceLabel];
    
    //商品数量
    x = 10;
    width = SCREEN_WIDTH - 3*x - width;
    UILabel *countLable = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    countLable.text = [NSString stringWithFormat:@"共%ld件商品",model.items.count];
    countLable.font = FONT(14);
    countLable.textColor = HTB_TEXT_COLOR2;
    [contentView addSubview:countLable];
    
    //线
    x = 0;
    y = 29;
    width = SCREEN_WIDTH;
    height = 1;
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    lineView.backgroundColor = HTB_LINE_COLOR;
    [contentView addSubview:lineView];
    return headerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetOrderModel *model = self.dataAry[indexPath.section];
    NSInteger proCount = model.items.count;
    if (indexPath.row < proCount) {
        ByOrderDetailVC *vc = [[ByOrderDetailVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
}
#pragma mark ---相关点击事件
/***去支付或者查看物流*/
- (void)rightClick:(UIButton *)btn{
    if ([btn.titleLabel.text isEqualToString:@"去支付"]) {
        NSLog(@"去支付");
    }else{
        NSLog(@"查看物流");
    }
}

/**确认收货*/
- (void)sureAction:(UIButton *)btn{
    NSLog(@"确认收货");
}

/**查看物流*/
- (void)lookAction:(UIButton *)btn{
    NSLog(@"查看物流");
}

@end
